﻿using IOT4Week1Day1.App.Dtos;
using IOT4Week1Day1.Domain.IRepositories;
using IOT4Week1Day1.Domain.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IOT4Week1Day1.App
{
    /// <summary>
    /// 地区模块功能
    /// </summary>
    public class AreaAppService : IAreaAppService
    {
        private readonly IRepository<AreaInfo> _repository;  //私有字段

        /// <summary>
        /// 构造函数（用来依赖注入）
        /// </summary>
        /// <param name="repository"></param>
        public AreaAppService(IRepository<AreaInfo> repository)
        {
            _repository = repository;
        }

        /// <summary>
        /// 获取省市区的递归树
        /// </summary>
        /// <returns></returns>
        public List<AreaTreeDto> GetAreaTree()
        {
            //1. 获取地区表的所有数据
            List<AreaInfo> data = _repository.GetTable()   //获取该表的IQueryable接口（相当于sql语句）
                                             .ToList();    //ToList才是真正的执行Sql语句 （延迟执行）

            //2. 把以上数据，组装成一个树结构数据( 递归方法）

            List<AreaTreeDto> result = LoadAreaTree(data);

            return result;
        }

        //带分页的
        //public (int total, List<AreaTreeDto> list) GetAreaTree(int pageIndex, int pageSize, string name)
        //{
        //    //1. 获取地区表的所有数据
        //    List<AreaInfo> data = _repository.GetTable().ToList();

        //    //2. 把以上数据，组装成一个树结构数据( 递归方法）
        //    List<AreaTreeDto> result = LoadAreaTree(data);

        //    //3. 分页查询处理
        //    result = result.Where(x => string.IsNullOrEmpty(name) || x.AreaName.Equals(name)).ToList();

        //    int total = result.Count();

        //    //1. 获取地区表的所有数据
        //    List<AreaTreeDto> list = result.OrderBy(x => x.Id)
        //                               .Skip((pageIndex - 1) * pageSize)
        //                               .Take(pageSize).ToList();
        //    return (total, list);
        //}

        /// <summary>
        /// 树形递归
        /// </summary>
        /// <param name="data"></param>
        /// <param name="pid"></param>
        /// <returns></returns>
        private List<AreaTreeDto> LoadAreaTree(List<AreaInfo> data, int pid = 0)
        {
            //1. 获取pid所对应的数据
            List<AreaInfo> childrenData = data.Where(x => x.PId == pid).ToList();
            //【终止条件，是递归的关键，否则，电脑死机】
            if (childrenData.Count == 0)  //如果无子级，那么停止 递归
            {
                return null;
            }

            List<AreaTreeDto> result = new List<AreaTreeDto>();

            //2.循环子级数据
            foreach (var item in childrenData)
            {
                AreaTreeDto one = new AreaTreeDto
                {
                    AreaName = item.AreaName,
                    Id = item.Id, //比如id为3，
                    Children = LoadAreaTree(data, item.Id) //去加载pid为3的数据，即主键3的子级
                };

                result.Add(one);
            }


            return result;
        }
    }
}
